package com.hundsun.yr.universal.p2p.datahandler;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.hundsun.yr.universal.library.common.LogUtils;
import com.hundsun.yr.universal.library.widget.HSToast;
import com.hundsun.yr.universal.p2p.config.ApiConstants2;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseActivity;

import java.lang.reflect.Method;

/**
 * 数据处理基类--用它的时候都涉及到反射
 * @author tonglj
 *
 */
public abstract class BaseDataHandler {
	public final String TAG = BaseDataHandler.class.getName();
	/**
	 * 上下文 
	 */
	protected Context mContext;
	/**
	 * 
	 */
	protected BaseActivity mBaseActivity;
	protected Activity mAc;
	/**
	 * 用于封装请求数据
	 */
//	protected ApiConstants mApiConstants;

	/**
	 * 用于封装第二类网络请求参数
	 */
	protected ApiConstants2 mApiConstants2;
	

	public BaseDataHandler(Context mContext) {
		this.mContext = mContext;
		try{
			mBaseActivity = (BaseActivity)mContext;
		}catch(Exception e){
			mAc = (Activity) mContext;
		}
//		mApiConstants = new ApiConstants(mContext);
	}
	
	protected void showToast(int resId){
		new HSToast(mContext).showSingletonToast(resId);
	}
	
	protected void showToast(String msg){
		new HSToast(mContext).showSingletonToast(msg);
	}
	
    /**
     * 跳转Activity并传递bundle
     *
     * @param clazz
     * @param bundle
     */
    protected void readyGo(Class<?> clazz, Bundle bundle) {
        Intent intent = new Intent(mContext, clazz);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        mContext.startActivity(intent);
    }
    
    protected void readyGoThenKill(Class<?> clazz, Bundle bundle) {
    	Intent intent = new Intent(mContext, clazz);
    	if (null != bundle) {
    		intent.putExtras(bundle);
    	}
    	mContext.startActivity(intent);
    	mBaseActivity.finish();
    }
    
    /**
     * 跳转Activity
     *
     * @param clazz
     */
    protected void readyGo(Class<?> clazz) {
        Intent intent = new Intent(mContext, clazz);
        mContext.startActivity(intent);
    }
    
    /**
     * 显示正在加载进度条
     */
    protected void showProgress(){
    	mBaseActivity.showProgress(null);
    }
    /**
     * 显示正在加载进度条
     *  msg 显示的文字
     */
    protected void showProgress(String msg){
    	mBaseActivity.showProgress(msg);
    }
    /**
     * 关闭正在加载进度条
     */
    protected void closeProgress(){
    	mBaseActivity.closeProgress();
    }
    
    
    
    
    /**
     * 处理后台事务 并显示加载中进度条
     * @param
     * @param methodName BaseDataHandler 子类函数方法名统一维护在methods_strings.xm
     * @param objs 传值
     */
    public void interactive(BaseDataHandler handler,int methodName,Object...objs){
    	showProgress();
    	interactiveSilent(handler, methodName, objs);
    }
    
    /**
     * 处理后台事务
     * @param
     * @param methodName BaseDataHandler 子类函数方法名 统一维护在methods_strings.xml
     * @param objs 传值
     */
    public void interactiveSilent(BaseDataHandler handler,int methodName,Object...objs){
    	try{
    		Class<? extends BaseDataHandler> clasz = handler.getClass();
    		//获得类的方法
			Method method = clasz.getDeclaredMethod(mContext.getString(methodName), Object[].class);
    		//调用该方法
    		method.invoke(handler, new Object[]{objs});
    	}catch(Exception e){
    		e.printStackTrace();
    		LogUtils.e(TAG, e.getMessage());
    		closeProgress();
    	}
    }
	
    
//    /**
//     * 获取fragment
//     * @param frag
//     * @return
//     */
//    public BaseFragment getFragment(T data){
//    	List<Fragment> list = mBaseActivity.getSupportFragmentManager().getFragments();
//    	for (Fragment fragment : list) {
//			if(fragment instanceof data){
//				return (BaseFragment) fragment;
//			}
//		}
//    	return null;
//    }
    
	
	
}
